# -*- coding: utf-8 -*-
'''
Created on Nov 1, 2010

@author: HoangNamHai
'''
import urllib
import cStringIO as StringIO
from lxml import etree
import time
import datetime
import os, traceback
from urllib import urlretrieve
from MongoDbLog import MongoDbLog
from MongoModel import MongoModel

stringify = etree.XPath("string()")
LOG = MongoDbLog('GiaVang', 'debug')

MONGO_SERVER = 'beta.mana.vn'
MONGO_PORT = 27017

class GiaVang(MongoModel):
    def __init__(self, host='localhost', port=27017):
        MongoModel.__init__(self, host, port)
        
    def saveImage(self, url, localFilePath):
        try:
            if not os.path.exists(os.path.dirname(localFilePath)):
                os.makedirs(os.path.dirname(localFilePath))
            a, b = urlretrieve(url, localFilePath)
            print a
            print b
            return localFilePath
        except:
            LOG.error(traceback.format_exc())
        return ''    
    
    def getGiaVang(self):
        LOG.debug('start getGiaVang')
        blocks = {"tg_1": "TPHCM","tg_2": "Hà Nội","tg_3": "Đà Nẵng","tg_4": "Nha Trang","tg_5": "Cần Thơ"}
        try:
            url = 'http://www.sjc.com.vn/ajax_vang.php?time={0}&session=18&n=0'.format(time.time())
            result = urllib.urlopen(url)
            html = result.read()
            parser = etree.HTMLParser(encoding='utf-8')
            tree = etree.parse(StringIO.StringIO(html), parser)
            data = []
            for blockName in blocks.keys():
                for blockElem in tree.xpath("//*[@id='{0}']//tr".format(blockName)):
                    dataRow = {}
                    dataRow['thanhpho'] = blocks[blockName]
                    dataRow['loai'] = stringify(blockElem.xpath('./td[1]')[0]).strip()
                    dataRow['mua'] = stringify(blockElem.xpath('./td[2]')[0]).strip()
                    dataRow['ban'] = stringify(blockElem.xpath('./td[3]')[0]).strip()
                    data.append(dataRow)
            result = {}
            result['reporttime'] = tree.xpath(unicode("//td[contains(.,'Cập nhật lúc')]".decode('utf-8')))[0].text
            result['_id'] = hash(result['reporttime'])
            result['data'] = data
            result['lastupdate'] = datetime.datetime.now()
            result['timestamp'] = time.time()
            
            db = self.connection['giavang']    
            collection = db['sjc']    
            collection.save(result)
            print result
        except:
            LOG.error(traceback.format_exc())
    
    def getBieudo(self):
        LOG.debug('start getBieudo')
        self.saveImage('http://www.sacombank-sbj.com/service/chartSBJ.ashx?temp={0}'.format(time.time()), '/home/hoangnamhai/HarvestedData/giavang/giavangJbs.jpg')
        self.saveImage('http://www.kitco.com/images/live/t24_au_en_usoz_home.gif?temp={0}'.format(time.time()), '/home/hoangnamhai/HarvestedData/giavang/vangThegioi.gif')
        return

if __name__ == '__main__':
    
    LOG.info('start crawler gia vang')
    gv = GiaVang(MONGO_SERVER, MONGO_PORT)
    gv.getGiaVang()
    gv.getBieudo()
    LOG.info('finished crawler gia vang')
    pid = os.getpid()
    os._exit(1)
    os.kill(pid, 9)
    
